package com.powernode.springmvc.controller;

import com.powernode.springmvc.bean.User;
import com.powernode.springmvc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.net.URI;

//@RestController (等同于@Controller + @ResponseBody)
@Controller
public class UserController {
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public String getAll(){
        System.out.println("正在查询所有用户信息....");
        return "ok";
    }
//
//    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
//    public String getById(@PathVariable("id") String id){
//        //意思是将/user/{id}这个id占位符传给方法的参数id
//        System.out.println("正在根据id查询用户信息，用户id是：" + id);
//        return "ok";
//    }

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public String save(User user){
        System.out.println("正在保存用户信息");
        System.out.println(user);
        return "ok";
    }

    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public String modify(User user){
        System.out.println("正在修改用户信息：" + user);
        return "ok";
    }
//
//    @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
//    public String del(@PathVariable("id") String id){
//        System.out.println("正在删除用户：" + id);
//        return "ok";
//    }


    @Autowired
    private UserService userService;

    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public ResponseEntity<User> getById(@PathVariable("id") Long id){
        User user = userService.getById(id);
        if(user == null){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }else{
            return ResponseEntity.ok(user);
        }
    }

}
